﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using Hidistro.Entities.Commodities;
    using Hidistro.SaleSystem.Catalog;
    using JSShop.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class wnxd_AttributeList : WebControl
    {
        private int brandId;
        private int categoryId;
        private string valueStr = string.Empty;

        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pn="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }

        protected override void OnInit(EventArgs e)
        {
            int.TryParse(this.Context.Request.QueryString["categoryId"], out this.categoryId);
            int.TryParse(this.Context.Request.QueryString["brand"], out this.brandId);
            base.OnInit(e);
        }

        private string html()
        {
            StringBuilder sb = new StringBuilder();
            List<JSShop.Model.Hishop_BrandCategories> brandCategories = new Juse.Interface.CategoryBrowser().GetBrandList(this.categoryId, 16);
            if ((brandCategories != null) && (brandCategories.Count > 0))
            {
                sb.AppendFormat("<div class=\"brand_story_pp_left\"><p>品牌：</p></div> <div class=\"brand_story_pp_right\"><p class=\"bnnn\">");

                if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
                {
                    sb.AppendFormat("<a href=\"{0}\"{1}>全部</a>", this.CreateUrl("brand", ""), this.brandId == 0 ? " class=\"kkokk\"" : "");
                    foreach (Hishop_BrandCategories row in brandCategories)
                    {
                        sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", this.CreateUrl("brand", row.BrandId.ToString()), row.BrandName, this.brandId == ((int)row.BrandId) ? " class=\"kkokk\"" : "");
                    }
                    sb.AppendFormat("</p></div>");
                }
                else
                {
                    sb.AppendFormat("<a href=\"{0}\"{1}>全部</a>", this.CreateStaticizeUrl(0), this.brandId == 0 ? " class=\"kkokk\"" : "");
                    foreach (Hishop_BrandCategories row in brandCategories)
                    {
                        sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", this.CreateStaticizeUrl(row.BrandId), row.BrandName, this.brandId == ((int)row.BrandId) ? " class=\"kkokk\"" : "");
                    }
                    sb.AppendFormat("</p></div>");
                }
            }

            return sb.ToString();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.html());
        }

        private string CreateStaticizeUrl(int pos)
        {
            int feature;
            int brand;
            int filter;
            int rank;

            int.TryParse(this.Page.Request.QueryString["feature"], out feature);
            int.TryParse(this.Page.Request.QueryString["brand"], out brand);
            int.TryParse(this.Page.Request.QueryString["filter"], out filter);
            int.TryParse(this.Page.Request.QueryString["rank"], out rank);

            int categoryId;
            
            int.TryParse(this.Page.Request.QueryString["categoryId"], out categoryId);


            CategoryInfo cateif = CategoryBrowser.GetCategory(categoryId);

            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.ToLower().Contains("/brand"))
            {
                if (cateif == null)
                    return Globals.GetSiteUrls().StaticizeBrand(0, filter, pos, rank, 0);
                else
                    return Globals.GetSiteUrls().StaticizeBrand(cateif.CategoryId, filter, pos, rank, 0);
            }
            else
            {
                if (cateif == null)
                    return Globals.GetSiteUrls().StaticizeSubCategory(0, "", 0, "", feature, pos, filter, rank, 0);
                else
                    return Globals.GetSiteUrls().StaticizeSubCategory(cateif.CategoryId, cateif.RewriteName, (int)cateif.ParentCategoryId, cateif.ParentRewriteName, feature, pos, filter, rank, 0);
            }
        }

    }
}

